using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public  class tbhinhthucthanhtoanDAL
    {
        private SqlConnection cn = null;
        public tbhinhthucthanhtoanDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbhinhthucthanhtoanDAL(tbhinhthucthanhtoanDAL thanhtoan )
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
       public void Insert(tbhinhthucthanhtoan thanhtoan)
        {
            SqlCommand cm = new SqlCommand("hinhthucthanhtoan_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tenhinhthucthanhtoan", thanhtoan.Tenhinhthucthanhtoan  );
            cm.ExecuteNonQuery();
        }
        public void Update(tbhinhthucthanhtoan thanhtoan)
        {
            SqlCommand cm = new SqlCommand("hinhthucthanhtoan_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mahinhthucthanhtoan",thanhtoan.Mahinhthucthanhtoan  );
            cm.Parameters.AddWithValue("@tenhinhthucthanhtoan", thanhtoan.Tenhinhthucthanhtoan  );
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("hinhthucthanhtoan_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mahinhthucthanhtoan", iD);
            cm.ExecuteNonQuery();
        }
        public tbhinhthucthanhtoancollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("hinhthucthanhtoan_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbhinhthucthanhtoancollection hinhthucthanhtoanCollection = new tbhinhthucthanhtoancollection();
            while (dr.Read())
            {
                tbhinhthucthanhtoan thanhtoan = new tbhinhthucthanhtoan();
                thanhtoan .Mahinhthucthanhtoan  = int.Parse(dr.GetValue(0).ToString());
                thanhtoan .Tenhinhthucthanhtoan   = (dr.GetValue(1).ToString());
                hinhthucthanhtoanCollection  .Add(thanhtoan  );
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return hinhthucthanhtoanCollection  ;
        }
    }
}
